package com.yscz.peexcel.controller;

import com.yscz.peexcel.common.Result;
import com.yscz.peexcel.config.YmlValueConfig;
import com.yscz.peexcel.entity.Option;
import com.yscz.peexcel.entity.Scale;
import com.yscz.peexcel.mapper.ScaleMapper;
import com.yscz.peexcel.service.PoiExcelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.yscz.peexcel.util.PoiExcelUtils.readExcelData;

/**
 * @author achen
 * @create 2021-12-01 14:40
 */
@RestController
@Slf4j
public class ReadExcelController {

    @Autowired
    private PoiExcelService poiExcelService;

    @Autowired
    private ScaleMapper scaleMapper;

    @GetMapping("/excel/readExcel")
    public Result readExcel() {
        poiExcelService.readScaleExcel();
        log.info("read excel end");
        return Result.success(200, "量表数据导入成功");
    }

    @GetMapping("/excel/test")
    public Result test() {
        List<Scale> scales = scaleMapper.selectList(null);
        Optional<Scale> max = scales.stream().max((scale1, scale2) -> Integer.compare(Integer.parseInt(scale1.getScaleSort()), Integer.parseInt(scale2.getScaleSort())));
        System.out.println(max);
        String scaleSort = null;
        if (!max.equals(Optional.empty())) {
            scaleSort = max.get().getScaleSort();
        }
        return Result.success(200, "", String.valueOf(Integer.parseInt(scaleSort) + 1));
    }
}
